﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using VisionLibaray;

namespace TestInterestingArea
{
    public partial class Form1 : Form
    {
        Size sizeSource, sizeVision, sizeBlock, sizeMove;
        Bitmap bmpSource = Properties.Resources.test, 
            bmpVision, bmpBoundary, bmpInterest, bmpOverlap;
        Point pCenter;
        Rectangle rectSource;
        public Form1()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint
                    | ControlStyles.OptimizedDoubleBuffer
                    | ControlStyles.ResizeRedraw
                    | ControlStyles.UserPaint
                    , true);

            InitializeComponent();

            sizeVision = new Size(352, 288);
            sizeBlock = new Size(4, 4);
            sizeMove = new Size(2, 2);
            sizeSource = bmpSource.Size;
            rectSource = new Rectangle(new Point(), sizeSource);
            bmpVision = new Bitmap(sizeVision.Width, sizeVision.Height);
            bmpBoundary = new Bitmap(sizeVision.Width, sizeVision.Height);
            bmpInterest = new Bitmap(sizeVision.Width, sizeVision.Height);
            bmpOverlap = new Bitmap(
                sizeVision.Width  / sizeMove.Width,
                sizeVision.Height / sizeMove.Height);
             
            pCenter = new Point(sizeSource.Width / 2, sizeSource.Height / 2);

            Processing();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics G = e.Graphics;

            try
            {
                G.DrawImage(bmpSource, 0, 0);

                G.DrawImage(bmpVision, sizeSource.Width, 0);
                G.DrawImage(bmpBoundary, sizeSource.Width, sizeVision.Height);
                G.DrawImage(bmpInterest, 0, sizeSource.Height);
                G.DrawImage(bmpOverlap, sizeVision.Width, sizeSource.Height, sizeVision.Width, sizeVision.Height);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }


        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (!rectSource.Contains(e.Location)) return;

            pCenter = e.Location;
            DateTime DT = DateTime.Now;

            Processing();

            TimeSpan TS = new TimeSpan(DateTime.Now.Ticks - DT.Ticks);
            this.Text = TS.Seconds + "秒 " + TS.Milliseconds + "毫秒";
            this.Invalidate();
        }

        void Processing()
        {
            ImageProcessing.Fuzzy(bmpSource, bmpVision, pCenter);
            ImageProcessing.Boundary(bmpVision, bmpBoundary);
            ImageProcessing.GreyEnhance(bmpVision, bmpInterest);
            ImageProcessing.Abstract(bmpVision, bmpOverlap, sizeBlock, sizeMove);
        }
    }
}
